#include "tthreadlistitem.h"
#include <iostream>

TThreadListItem::TThreadListItem(TThreadListItem *parent) :
    QTreeWidgetItem(parent)
{
}

TThreadListItem::TThreadListItem(QUrl URL, QString ThreadName, TThreadListItem *parent) :
    QTreeWidgetItem(parent),
    threadURL(URL),
    threadName(ThreadName)
{
    std::cout << "Echo 4" << std::endl;
    if(this->isDisabled()) this->setDisabled(false);
    this->setText(0, "Patience...");
    this->setText(1, "Patience...");
    this->setDisabled(true);
    std::cout << "Echo 5" << std::endl;
}

void TThreadListItem::update(void){
    this->setText(0, watchedThread->getName());
    this->setTextColor(0, QColor(Qt::black));
    this->setText(1, watchedThread->getState());
    this->setTextColor(1, watchedThread->getColor());
    if(this->isDisabled()) this->setDisabled(false);
}

void TThreadListItem::startWatch(void){
    watchedThread->startWatch();
}

void TThreadListItem::stoptWatch(void){
    watchedThread->stopWatch();
}

void TThreadListItem::activate(void){
    std::cout << "Echo 6" << std::endl;
    if (!threadName.isNull()){
        watchedThread = new TThread(threadURL, threadName);
    }else{
        watchedThread = new TThread(threadURL, NULL);
    }
    std::cout << "Echo 6.5" << std::endl;
    QObject::connect(watchedThread, SIGNAL(isChanged()), this, SLOT(update()));
    QObject::connect(watchedThread, SIGNAL(isChanged()), this, SIGNAL(isChanged()));
    update();
    emit isChanged();
    std::cout << "Echo 7" << std::endl;
}

void TThreadListItem::rename(QString newName){
    watchedThread->setName(newName);
}

QUrl TThreadListItem::getURL(void){
    return watchedThread->getURL();
}

QString TThreadListItem::getName(void){
    return watchedThread->getName();
}
